import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:photo/photo.dart';
import 'package:photo_manager/photo_manager.dart';
//import 'package:multi_image_picker/multi_image_picker.dart'; 多图三退
class ImagePickePage extends StatefulWidget {
  ImagePickePage({Key key}) : super(key: key);

  @override
  _ImagePickePageState createState() => _ImagePickePageState();
}

class _ImagePickePageState extends State<ImagePickePage> {


  File _image;
  // List<Asset> images = List<Asset>();
  // String _error = 'No Error Dectected';

  /**
   * 拍照
   */
  _takePhoto() async{
    var image = await ImagePicker.pickImage(source: ImageSource.camera,maxWidth: 400);
    print(image);
    setState(() {
      this._image = image;
    });
  }

  /**
   * 图库选择
   */
   _takeImage() async{
    var image = await ImagePicker.pickImage(source: ImageSource.gallery,maxWidth: 400);
    print(image);
    setState(() {
      this._image = image;
    });

    this._uploading();

  }
  //多图选择
  void pickAssets() async {
    List<AssetEntity> assetList = await PhotoPicker.pickAsset(context: context,rowCount:3,thumbSize:500,padding:5);
    print(assetList);
    /// Use assetList to do something.
  }
  ///权限检查
//  Future<bool> _checkPermission() async {
//    // 先对所在平台进行判断
//    if (Theme.of(context).platform == TargetPlatform.android) {
//      PermissionStatus permission = await PermissionHandler()
//          .checkPermissionStatus(PermissionGroup.storage);
//      if (permission != PermissionStatus.granted) {
//        Map<PermissionGroup, PermissionStatus> permissions =
//        await PermissionHandler()
//            .requestPermissions([PermissionGroup.storage]);
//        if (permissions[PermissionGroup.storage] == PermissionStatus.granted) {
//          return true;
//        }
//      } else {
//        return true;
//      }
//    } else {
//      return true;
//    }
//    return false;
//  }
  // Future<void> loadAssets() async {
  //   List<Asset> resultList = List<Asset>();
  //   String error = 'No Error Dectected';

  //   try {
  //     resultList = await MultiImagePicker.pickImages(
  //       maxImages: 6,
  //       enableCamera: true,
  //       selectedAssets: images,
  //       cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
  //       materialOptions: MaterialOptions(
  //         actionBarColor: "#abcdef",
  //         actionBarTitle: "Example App",
  //         allViewTitle: "All Photos",
  //         useDetailsView: false,
  //         selectCircleStrokeColor: "#000000",
  //       ),
  //     );
  //   } on Exception catch (e) {
  //     error = e.toString();
  //   }

  //   // If the widget was removed from the tree while the asynchronous platform
  //   // message was in flight, we want to discard the reply rather than calling
  //   // setState to update our non-existent appearance.
  //   if (!mounted) return;

  //   setState(() {
  //     images = resultList;
  //     _error = error;
  //   });
  // }
  Widget buildImade(){
    if(this._image == null){
      return Text("请选择图片");
    }else{
      return Image.file(this._image);
    }
  }
  _uploading()async{
    FormData formData = new FormData.from({
          "name":"wendiux",
          "file":new UploadFileInfo(this._image, "xxx")
    });
    var res = await Dio().post("http://jd.itying.com/imgupload",data:formData);
    print(res);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("拍照选择相册"),
      ),
      body: Column(
        children: <Widget>[
          FlatButton(
            child: Text("拍照"),
            onPressed: this._takePhoto,
          ),
          FlatButton(
            child: Text("图库选择"),
            onPressed: this._takeImage,
          ),
          RaisedButton(
            child: Text("多图"),
            onPressed: this.pickAssets,
          ),
          buildImade()
        ],
      ),
    );
  }
}
